//
//  LXDAllSOngViewController.m
//  BLDMusics
//
//  Created by lxd on 16/3/22.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "LXDAllSOngViewController.h"

@interface LXDAllSOngViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
/**
 *  五个分区的数组
 */
@property (nonatomic, retain) NSMutableArray *modelArr;

/**
 *  collection
 */
@property (nonatomic, retain) UICollectionView *collection;

@property (nonatomic, strong) UIVisualEffectView *visualEfView; /**< 毛玻璃 */

@end

@implementation LXDAllSOngViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createCollection];
    [self dataHandle];
    
}

#pragma mark - 创建collection
- (void)createCollection {
    
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ground.jpg"]];
    // 毛玻璃
    self.visualEfView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    _visualEfView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    _visualEfView.alpha = 0.8;
    _visualEfView.userInteractionEnabled = YES;
    [self.view addSubview:_visualEfView];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(5, 10, 15, 10);
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH - 64 - 60) collectionViewLayout:layout];
    _collection.dataSource = self;
    _collection.delegate = self;
    [self.view addSubview:_collection];
    [_collection registerClass:[LXDAllSongCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_collection registerClass:[LXDAllCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header1"];
    _collection.backgroundColor = [UIColor clearColor];
    
}

#pragma mark - 协议方法
#pragma mark 返回分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (_modelArr.count) {
        return _modelArr.count + 1;
    } else {
        return 0;
    }
}
#pragma mark 返回每个分区的行数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    LXDAllSongModel *model = _modelArr[section -1];
    return model.children.count;
}
#pragma mark 返回item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        LXDAllSongCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.textLable.text = @"全部";
        return cell;
    }
    LXDAllSongCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    LXDAllSongModel *model = _modelArr[indexPath.section - 1];
    cell.model = model.children[indexPath.row];
    return cell;
}
#pragma mark - 返回分区头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section > 0 && kind == UICollectionElementKindSectionHeader) {
        LXDAllCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.model = _modelArr[indexPath.section - 1];
        return header;
    }
    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header1" forIndexPath:indexPath];
    return header;
}
#pragma mark 设置头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH, 30);
}
#pragma mark 设置item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(SCREEN_WIDTH - 20, 40);
    }
    return CGSizeMake((SCREEN_WIDTH - 35) / 4, (SCREEN_WIDTH - 35) / 4 / 2);
}


#pragma mark - item的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        _block(@"");
    } else {
       LXDAllSongModel *mdel = _modelArr[indexPath.section - 1];
       LXDAllSongModel *modle = mdel.children[indexPath.row];
       _block(modle.tag);
    }
    [self.navigationController popViewControllerAnimated:YES];
}





#pragma mark - 数据解析
- (void)dataHandle {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [APPTools GETWithURL:@"http://api.dongting.com/song/tag/songlist?" par:nil success:^(id responseObject) {
        self.modelArr = [LXDAllSongModel modelHandleWithArray:responseObject[@"data"]];
        [_collection reloadData];
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    } filed:^(NSError *error) {
        
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
